/*
 * File:   RunTestSsytrd.cpp
 * Author: vvs
 *
 * Created on Mar 14, 2012, 10:02:36 AM
 */

#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>

#include "../../Profiler.h"

struct ProfilingListener : CppUnit::TestListener {
    void startTest(CppUnit::Test* test) {
        Profiler::clear();
        Profiler::start(test->getName() + " time");        
    }
    
    void endTest(CppUnit::Test* test) {
        Profiler::stop(test->getName() + " time"); 
        std::cout << Profiler::print() << std::endl;
    }
};

int main() {
    // Create the event manager and test controller    
    CPPUNIT_NS::TestResult controller;
        
    // Add a listener that collects test result
    CPPUNIT_NS::TestResultCollector result;
    controller.addListener(&result);

    // Add a listener that print dots as test run.
    CPPUNIT_NS::BriefTestProgressListener progress;
    controller.addListener(&progress);
    
    // Add profiling listener
    ProfilingListener listener;
    controller.addListener(&listener);

    // Add the top suite to the test runner
    CPPUNIT_NS::TestRunner runner;
    runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
    runner.run(controller);

    // Print test in a compiler compatible format.
    CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut());
    outputter.write();

    return result.wasSuccessful() ? 0 : 1;
}
